package view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Created by eyesee on 2017/5/19.
 */

public class SelectListview extends ListView implements View.OnKeyListener {
    private int mDuration;
    private ListAdapter mAdapter;
    private int mItemCount;
    private int mItemHeight;

    public SelectListview(Context context) {
        super(context);
    }

    public SelectListview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnKeyListener(this);
        this.setSmoothScrollbarEnabled(true);
    }

    public SelectListview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //滚动动画时间
    public void setScrollDuration(int duration ){
        mDuration = duration;
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
        this.mAdapter = adapter;
        mItemCount = mAdapter.getCount();                   //item数量
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (mAdapter != null){
            mItemHeight = this.getChildAt(0).getHeight();   //item的高度
        }
    }

    /**记录上一个被选中的item,返回时默认该item被选中*/
    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        int position = getSelectedItemPosition();
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        if (gainFocus){
            setSelection(position);
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_UP){
            return false;
        }
        int curPositon = this.getSelectedItemPosition();    //当前被选中的item的position

        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
            if (curPositon == mItemCount-1){
                setSelection(0);
                return false;
            }/*else {
                this.smoothScrollToPositionFromTop(curPositon+1,this.getHeight()/2,mDuration);
                return false;
            }*/
        }

        if (keyCode == KeyEvent.KEYCODE_DPAD_UP){
            if (curPositon ==1){
                if (this.getChildAt(0).isFocusable()==false){
                    this.smoothScrollBy(-mItemHeight,mDuration);
                }
                return false;
            }else if (curPositon == 0){
                setSelection(mItemCount-1);
                return false;
            }/*else {
                this.smoothScrollToPositionFromTop(curPositon-1,this.getHeight()/2,mDuration);      //保持选中的item在屏幕中间
                return false;
            }*/
        }
        return false;
    }

    //平滑移动到最后
    /*public void smoothScrollToBottom(){
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                SelectListview.this.post(new Runnable() {
                    @Override
                    public void run() {
                        SelectListview.this.setSelection(SelectListview.this.getLastVisiblePosition());
                    }
                });
            }
        }, mDuration / 3);
    }


    //滚动到底部的监听
    public interface onScrollBottomListener{
        void onScrollBottom();
    }

    public void setOnScrollBottomListener(onScrollBottomListener scrollBottomListener){
        this.mOnscrollBottomListener = scrollBottomListener;
    }

    //滚动到顶部的监听
    public interface onScrollTopListener{
        void onScrollTop();
    }
    public void setOnScrollTopListener(onScrollTopListener scrollTopListener){
        mIsScrollTop = true;
        this.mOnScrollTopListener = scrollTopListener;
    }*/
}
